<?php
	//页面头部
	include ('header.php');
	//页面格式
	header('content-type:text/html;charset=utf8');
	//header('content-type:application/json;charset=utf8');
	
	//检查session
	if(!isset($_SESSION)){
    session_start();
	}
	$agent = check_wap();
	if($agent){
	
	}else{
		
		include ('caidanlan.php');
}
	
	$cid = $_SESSION["cid"];
	
	
	//修改车辆状态,获取值操作数据库
	if(isset($_GET["xuhao1"]))
	{
		$xuhao1=$_GET["xuhao1"];
	}
	else
	{
		$xuhao1="";
	}
	if(isset($_GET["chehao1"]))
	{
		$uodatechehao1=$_GET["chehao1"];
	}
	else
	{
		$uodatechehao1="";
	}
	if(isset($_GET["beizhu1"]))
	{
		$uodatebeizhu1=$_GET["beizhu1"];
	}
	else
	{
		$uodatebeizhu1="";
	}
	if(isset($_GET["jiashiyuan1"]))
	{
		$uodatejiashiyuan1=$_GET["jiashiyuan1"];
	}
	else
	{
		$uodatejiashiyuan1="";
	}
	if(isset($_GET["zhuangtai1"]))
	{
		$uodatezhuangtai1=$_GET["zhuangtai1"];
	}
	else
	{
		$uodatezhuangtai1="";
	}
	if($uodatechehao1=="" || $uodatechehao1==null and $uodatebeizhu1=="" || $uodatebeizhu1==null and $uodatejiashiyuan1=="" || $uodatejiashiyuan1==null and $uodatezhuangtai1=="" || $uodatezhuangtai1==null and $xuhao1=="" || $xuhao1==null)
	{
		
	}
	else if($uodatechehao1=="" || $uodatechehao1==null)
	{
		
	}
	else if($uodatebeizhu1=="" || $uodatebeizhu1==null)
	{
		
	}
	else if($uodatejiashiyuan1=="" || $uodatejiashiyuan1==null)
	{
		
	}
	else if($uodatezhuangtai1=="" || $uodatezhuangtai1==null)
	{
		
	}
	else if($xuhao1=="" || $xuhao1==null)
	{
		
	}
	else
	{		
		
		$sqlw1="select Id from a03zhuangtai where zhuangtai=\"$uodatezhuangtai1\" and c_id=\"$cid\" and yingcang=\"1\"";
		$sqlw2=mysql_query($sqlw1);
		$sqla3=mysql_fetch_array($sqlw2);
		
		$zhuangtaiid=$sqla3["Id"];
		$updatesqlq1="update a01cheliangzhuangtai set 
		chehao=\"$uodatechehao1\",
		beizhu=\"$uodatebeizhu1\",
		jiashiyuan=\"$uodatejiashiyuan1\",
		zhuangtai=\"$zhuangtaiid\" where c_id=\"$cid\" and xuhao=\"$xuhao1\"";
		mysql_query($updatesqlq1);
		
		$sqle1="select xuhao from a01cheliangzhuangtai where chehao=\"$uodatechehao1\" and
		beizhu=\"$uodatebeizhu1\" and
		jiashiyuan=\"$uodatejiashiyuan1\" and
		zhuangtai=\"$zhuangtaiid\" and c_id=\"$cid\"";
		//echo $sqle1;
		$sqle2=mysql_query($sqle1);
		$sqle3=mysql_num_rows($sqle2);
		
		if($sqle3>0)
		{
			echo "<script>"."alert('修改车辆信息成功')"."</script>";
			redirect('chemanage.php');
		}
		else
		{
			echo "<script>"."alert('修改车辆信息失败')"."</script>";
			redirect('chemanage.php');
		}
		
	}
	
	
	
	
	//删除车辆
	if (isset($_GET["account1"]))
 	{
		$account1=$_GET["account1"];
	}
	else
	{
		$account1="";
	}
	if($account1=="" || $account1==null)
	{
		
	}
	else if($account1=="" || $account1==null)
	{
		
	}else
	{
		mysql_select_db("che2");
		mysql_query("set names 'utf8'");
		$deletesql="update a01cheliangzhuangtai set shanchu=\"0\" where xuhao=\"$account1\" and c_id=\"$cid\"";
		mysql_query($deletesql);
		
		$sqla1="select xuhao from a01cheliangzhuangtai where xuhao=\"$account1\" and c_id=\"$cid\" and shanchu=\"1\"";
		$sqla2=mysql_query($sqla1);
		$sqla3=mysql_num_rows($sqla2);
		if($sqla3>0)
		{
			echo "<script>"."alert('删除失败')"."</script>";
			redirect('chemanage.php');
		}else
		{
			echo "<script>"."alert('删除成功')"."</script>";
			redirect('chemanage.php');
		}
	}
	
	//添加车辆
	if (isset($_GET["chehao"]))
 	{
		$chehao=$_GET["chehao"];
	}
	else
	{
		$chehao="";
	}
	if (isset($_GET["jiashiyuan"]))
 	{
		$jiashiyuan=$_GET["jiashiyuan"];
	}
	else
	{
		$jiashiyuan="";
	}
	if (isset($_GET["beizhu"]))
 	{
		$beizhu=$_GET["beizhu"];
	}
	else
	{
		$beizhu="";
	}
	if (isset($_GET["zhuangtai"]))
 	{
		$zhuangtai=$_GET["zhuangtai"];
	}
	else
	{
		$zhuangtai="";
	}
	if($chehao=="" || $chehao ==null and $jiashiyuan=="" || $jiashiyuan ==null and $zhuangtai=="" || $zhuangtai ==null and $beizhu=="" || $beizhu ==null)
	{
		
	}
	else if($chehao=="" || $chehao ==null)
	{
		
	}
	else if($jiashiyuan=="" || $jiashiyuan ==null)
	{
		
	}
	else if($beizhu=="" || $beizhu ==null)
	{
		
	}
	else if($zhuangtai=="" || $zhuangtai ==null)
	{
		
	}
	else
	{
		$querychesqla1="select chehao from a01cheliangzhuangtai where c_id=\"$cid\" and chehao=\"$chehao\"";
		$querychesqla2=mysql_query($querychesqla1);
		$querychesqla3=mysql_num_rows($querychesqla2);
		if($querychesqla3>0)
		{
			echo "<script>"."alert('该车辆已存在')"."</script>";
			redirect('chemanage.php');
		}
		else
		{
			$s3 = "select Id from a03zhuangtai where zhuangtai =\"$zhuangtai\" and c_id = \"$cid\"";
			//echo $s3;
			$c3 = mysql_query($s3);
			$zt_id="";
			while ($f3 = mysql_fetch_row($c3)) 
			{
				$zt_id = $f3[0];
				//echo $zt_id;
			}
			
			$sqlr1="select uid from a00user where uname=\"$jiashiyuan\" and c_id=\"$cid\"";
			$sqlr2=mysql_query($sqlr1);
			$sqlr3=mysql_fetch_array($sqlr2);
			
			$unameid=$sqlr3["uid"];
			
			if($unameid=="" or $unameid==null)
			{
				echo "<script>"."alert('公司没有该用户，请先添加')"."</script>";
				redirect('chemanage.php');
			}
			else
			{
			$addsqla1="insert into a01cheliangzhuangtai(uid,beizhu,chehao,jiashiyuan,c_id,zhuangtai)values('$unameid','$beizhu','$chehao','$jiashiyuan','$cid','$zt_id')";
			mysql_query($addsqla1);
			
			$querychesqlb1="select chehao from a01cheliangzhuangtai where c_id=\"$cid\" and chehao=\"$chehao\"";
			$querychesqlb2=mysql_query($querychesqlb1);
			$querychesqlb3=mysql_num_rows($querychesqlb2);
			
			if ($querychesqlb3> 0)
			 {
				// echo "添加车辆信息成功";
				 echo "<script>"."alert('添加车辆信息成功')"."</script>";
				 redirect('chemanage.php');
			}
			else
			{
				// echo "添加车辆信息失败!";
				 echo "<script>"."alert('添加车辆信息失败')"."</script>";
				 redirect('chemanage.php');
			}
				
			}
			
			
		}
		
	}	
	
	
	mysql_select_db("che2");
	mysql_query("set names 'utf8'");
	
	if(isset($_GET["carnumber"]) and $_GET["carnumber"]!="" and $_GET["carnumber"]!=null)
	{
		$carnumber=$_GET["carnumber"];
		
		$carsql="SELECT
a01cheliangzhuangtai.xuhao,
a01cheliangzhuangtai.chehao,
a01cheliangzhuangtai.beizhu,
a01cheliangzhuangtai.jiashiyuan,
a03zhuangtai.zhuangtai
FROM
a01cheliangzhuangtai
INNER JOIN a03zhuangtai ON a01cheliangzhuangtai.zhuangtai = a03zhuangtai.Id AND a01cheliangzhuangtai.c_id = a03zhuangtai.c_id
where a01cheliangzhuangtai.c_id=\"$cid\" and a01cheliangzhuangtai.shanchu=\"1\" and a01cheliangzhuangtai.chehao LIKE '%$carnumber%'";
	}
	else
	{
	$carsql="SELECT
a01cheliangzhuangtai.xuhao,
a01cheliangzhuangtai.chehao,
a01cheliangzhuangtai.beizhu,
a01cheliangzhuangtai.jiashiyuan,
a03zhuangtai.zhuangtai
FROM
a01cheliangzhuangtai
INNER JOIN a03zhuangtai ON a01cheliangzhuangtai.zhuangtai = a03zhuangtai.Id AND a01cheliangzhuangtai.c_id = a03zhuangtai.c_id
where a01cheliangzhuangtai.c_id=\"$cid\" and a01cheliangzhuangtai.shanchu=\"1\"";

	}
echo <<<end5
<a href="address.php">
<i class="icon-home icon-2x"></i> 
</a>
<i class=" icon-caret-right icon-large"></i>

<a href="chemanage.php">车辆管理</a>

end5;


$carsql1=mysql_query($carsql);
	echo <<<end1
	<div class="well">
	 <a href="pltjzhuangtai.php"><span class="zitiyangshi"> 批量添加车辆</span></a>
	 
	<br>
	<br>
	<form action="chemanage.php" method="GET">

	<div>车牌号:
	<input class="text" name="carnumber">
	</div>

	<input type="submit" values="提交"> 
	
	</form>
	<table class="table">
	
	<table class="table">
	<tr>
	<th>序号</th>
	<th>车号</th>
	<th>备注</th>
	<th>驾驶员</th>
	<th>状态</th>
	<th>操作</th>
	</tr>
	
end1;
	
	while ($carsql2=mysql_fetch_row($carsql1))
	{
		echo "<tr>";

		echo "<td>$carsql2[0]</td>";
		echo "<td>$carsql2[1]</td>";
		echo "<td>$carsql2[2]</td>";
		echo "<td>$carsql2[3]</td>";
		echo "<td>$carsql2[4]</td>";
		echo "<td><a href='javascript:void(0)' onclick='update(this);' title='添加车辆'><i class='icon-plus icon-large'></i></a>".
		"&nbsp;&nbsp;<a href='chemanage.php?account1=$carsql2[0]' title='删除车辆'><i class='icon-remove icon-large'></i></a>".
		"&nbsp;&nbsp;<a href='javascript:void(0)' onclick='update1(this);' title='修改信息'><i class='icon-cog icon-large'></i></a></td>";
		echo "</tr>";
	}
	
	echo "</table></div>";
	//添加车辆模态框
	echo <<<end2
	<div class="modal fade" id="myModala2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
		<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					添加车辆
				</h4>
			</div>
			<div class="modal-body">
			<form action ="chemanage.php" method="GET">
			车&nbsp;&nbsp;牌&nbsp;&nbsp;号：
			<input type="text" name="chehao"><br>
 			驾&nbsp;&nbsp;驶&nbsp;&nbsp;员：
 			<input type="text" name="jiashiyuan"><br>
			备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp注：
 			<input type="text" name="beizhu"><br>
 			车辆状态：
			 <select name = "zhuangtai">
end2;

 			$s2 = "select zhuangtai from a03zhuangtai where c_id = \"$cid\" and yingcang=\"1\"";
			 //echo $s2;
 			$c2 = mysql_query($s2);
 			while ($f2 = mysql_fetch_row ($c2))
			 {
 	 			foreach ($f2 as $ff2)
 	 		 {
			echo "<option value = '$ff2'>"."$ff2"."</option>";
			 }
			}
			echo <<<end3
			</select><br>
			如没有车辆状态，<a href="tianjiazhuangtai.php">请点击我添加</a><br>
 			<input type="submit" value="提交">
 			</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭
				</button>
		
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

end3;

	
		echo <<<end4
	<div class="modal fade" id="myModala1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
		<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					修改信息
				</h4>
			</div>
			<div class="modal-body">
			<form action ="chemanage.php" method="GET">
			序号：
			<input type="text" name="xuhao1" id="xuhao1" readonly="readonly"><br>
			车&nbsp;&nbsp;牌&nbsp;&nbsp;号：
			<input type="text" name="chehao1" id="chehao1"><br>
			备注：
			<input type="text" name="beizhu1" id="beizhu1"><br>
 			驾&nbsp;&nbsp;驶&nbsp;&nbsp;员：
 			<input type="text" name="jiashiyuan1" id="jiashiyuan1"><br>
 			车辆状态：
			 <select name ="zhuangtai1" id="zhuangtai1">
end4;

 			$s2 = "select zhuangtai from a03zhuangtai where c_id = \"$cid\" and yingcang=\"1\"";
			 //echo $s2;
 			$c2 = mysql_query($s2);
 			while ($f2 = mysql_fetch_row ($c2))
			 {
 	 			foreach ($f2 as $ff2)
 	 		 {
			echo "<option value = '$ff2'>"."$ff2"."</option>";
			 }
			}
			echo <<<end5
			</select><br>
			
 			<input type="submit" value="提交">
 			</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭
				</button>
		
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

end5;



echo <<<enda1
	<script src="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
	<script>
	function update(obj){
		var tds=$(obj).parent().parent().find('td');
		
		$('#yonghu1').val(tds.eq(0).text());		
		$('#yonghu2').val(tds.eq(1).text());
		
		$('#myModala2').modal('show');
		
	}
	</script>

	<script>
	function update1(obj){
		var tds=$(obj).parent().parent().find('td');
		
		$('#xuhao1').val(tds.eq(0).text());
		$('#chehao1').val(tds.eq(1).text());		
		$('#beizhu1').val(tds.eq(2).text());
		$('#jiashiyuan1').val(tds.eq(3).text());
		$('#zhuangtai1').val(tds.eq(4).text());
		
		$('#myModala1').modal('show');
		
	}
</script>


enda1;

include 'bottom.php';
	
?>